package io.renren.modules.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.JacksonUtils;
import io.renren.modules.edu.dao.TEduTeacherinfoDao;
import io.renren.modules.edu.dto.EChartsDTO;
import io.renren.modules.edu.dto.TEduTeacherinfoDTO;
import io.renren.modules.edu.entity.TEduDepartmentEntity;
import io.renren.modules.edu.entity.TEduTeacherinfoEntity;
import io.renren.modules.edu.service.TEduDepartmentService;
import io.renren.modules.edu.service.TEduTeacherinfoService;
import io.renren.modules.edu.vo.TEduTeacherinfoVO;
import io.renren.modules.security.SecurityUser;
import io.renren.modules.security.UserDetail;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 教师信息表
 *
 * @author zwp zwp
 * @since 1.0.0 2022-07-11
 */
@Slf4j
@Service
public class TEduTeacherinfoServiceImpl extends ServiceImpl<TEduTeacherinfoDao, TEduTeacherinfoEntity> implements TEduTeacherinfoService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TEduDepartmentService tEduDepartmentService;

    @Override
    public void save(TEduTeacherinfoDTO tEduTeacherinfoDTO) {
        TEduTeacherinfoEntity tEduTeacherinfoEntity = JacksonUtils.obj2pojo(tEduTeacherinfoDTO, TEduTeacherinfoEntity.class);
        this.baseMapper.insert(tEduTeacherinfoEntity);
    }

    @Override
    public void update(TEduTeacherinfoDTO tEduTeacherinfoDTO) {
        TEduTeacherinfoEntity tEduTeacherinfoEntity = JacksonUtils.obj2pojo(tEduTeacherinfoDTO, TEduTeacherinfoEntity.class);
        this.updateById(tEduTeacherinfoEntity);
    }

    @Override
    public TEduTeacherinfoVO queryTeacherInfoById(Long id) {
        log.info("获取教师信息");
        TEduTeacherinfoEntity tEduTeacherinfoEntity = this.baseMapper.selectById(id);
        TEduTeacherinfoVO tEduTeacherinfoVO = JacksonUtils.obj2pojo(tEduTeacherinfoEntity, TEduTeacherinfoVO.class);
        SysUserEntity sysUserEntity = sysUserService.getById(tEduTeacherinfoVO.getSysUserId());
        tEduTeacherinfoVO.setRealName(sysUserEntity.getRealName());
        tEduTeacherinfoVO.setEmail(sysUserEntity.getEmail());
        tEduTeacherinfoVO.setMobile(sysUserEntity.getMobile());
        return tEduTeacherinfoVO;
    }

    @Override
    public TEduTeacherinfoVO queryTeacherInfo(Map<String, Object> params) {
        log.info("获取登录的教师信息");
        // 获取当前用户
        UserDetail user = SecurityUser.getUser();
        // 查询当前教师信息
        QueryWrapper<TEduTeacherinfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_user_id",user.getId());
        TEduTeacherinfoEntity tEduTeacherinfoEntity = this.baseMapper.selectOne(wrapper);
        // 查询部门名称
        TEduDepartmentEntity eduDepartmentEntity = tEduDepartmentService.getById(tEduTeacherinfoEntity.getEduDepartmentId());
        // 数据注入
        TEduTeacherinfoVO tEduTeacherinfoVO = JacksonUtils.obj2pojo(tEduTeacherinfoEntity, TEduTeacherinfoVO.class);
        tEduTeacherinfoVO.setUsername(user.getUsername());
        tEduTeacherinfoVO.setRealName(user.getRealName());
        tEduTeacherinfoVO.setEmail(user.getEmail());
        tEduTeacherinfoVO.setMobile(user.getMobile());
        tEduTeacherinfoVO.setEduDepartmentName(eduDepartmentEntity.getDepartmentName());
        if (user.getGender().equals(0)) {
            tEduTeacherinfoVO.setGenderName("男");
        } else if (user.getGender().equals(1)) {
            tEduTeacherinfoVO.setGenderName("女");
        } else {
            tEduTeacherinfoVO.setGenderName("保密");
        }
        return tEduTeacherinfoVO;
    }

    @Override
    public void onSubmit(TEduTeacherinfoDTO dto) {
        log.info("录入教师信息");
        TEduTeacherinfoEntity tEduTeacherinfoEntity = JacksonUtils.obj2pojo(dto, TEduTeacherinfoEntity.class);
        this.baseMapper.insert(tEduTeacherinfoEntity);
    }

    @Override
    public TEduTeacherinfoVO queryTeacherBySysUserId(Long sysUserId) {
        log.info("根据系统用户ID获取教师信息");
        QueryWrapper<TEduTeacherinfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_user_id", sysUserId);
        TEduTeacherinfoEntity tEduTeacherinfoEntity = this.baseMapper.selectOne(wrapper);
        return JacksonUtils.obj2pojo(tEduTeacherinfoEntity, TEduTeacherinfoVO.class);
    }

    @Override
    public TEduTeacherinfoVO queryTeacherByRealName(String realName) {
        log.info("根据系统用户姓名获取教师信息");
        SysUserEntity sysUserEntity = sysUserService.queryByRealName(realName);
        if (sysUserEntity == null) {
            throw new RRException(realName + "不存在！");
        }
        return this.queryTeacherBySysUserId(sysUserEntity.getId());
    }

    @Override
    public List<EChartsDTO> queryTeacherJob(Map<String, Object> params) {
        log.info("根据教师职称进行分类");
        List<TEduTeacherinfoEntity> eduTeacherinfoEntityList = this.baseMapper.selectList(null);
        for (TEduTeacherinfoEntity tEduTeacherinfoEntity : eduTeacherinfoEntityList) {
            SysUserEntity sysUserEntity = sysUserService.getBaseMapper().selectById(tEduTeacherinfoEntity.getSysUserId());
            tEduTeacherinfoEntity.setRealName(sysUserEntity.getRealName());
        }
        // 分组
        Map<String, List<TEduTeacherinfoEntity>> jobMap = eduTeacherinfoEntityList.stream().collect(Collectors.groupingBy(e -> e.getJobEvaluation()));
        List<EChartsDTO> chartsDTOList = new ArrayList<>();
        jobMap.keySet().forEach(e -> {
            List<TEduTeacherinfoEntity> teacherinfoEntityList = jobMap.get(e);
            if (CollectionUtils.isNotEmpty(teacherinfoEntityList)) {
                List<String> realNameList = teacherinfoEntityList.stream().map(j -> j.getRealName()).collect(Collectors.toList());
                EChartsDTO eChartsDTO = new EChartsDTO();
                eChartsDTO.setValue(teacherinfoEntityList.size());
                eChartsDTO.setName(e);
                eChartsDTO.setDataList(realNameList);
                chartsDTOList.add(eChartsDTO);
            }
        });
        return chartsDTOList;
    }

    @Override
    public List<EChartsDTO> queryDepartmentJob(Map<String, Object> params) {
        log.info("获取部门概况分类");
        List<TEduTeacherinfoEntity> eduTeacherinfoEntityList = this.baseMapper.selectList(null);
        for (TEduTeacherinfoEntity tEduTeacherinfoEntity : eduTeacherinfoEntityList) {
            SysUserEntity sysUserEntity = sysUserService.getBaseMapper().selectById(tEduTeacherinfoEntity.getSysUserId());
            tEduTeacherinfoEntity.setRealName(sysUserEntity.getRealName());
            TEduDepartmentEntity tEduDepartmentEntity = tEduDepartmentService.getById(tEduTeacherinfoEntity.getEduDepartmentId());
            tEduTeacherinfoEntity.setEduDepartmentName(tEduDepartmentEntity.getDepartmentName());
        }
        // 分组
        Map<String, List<TEduTeacherinfoEntity>> departmentMap = eduTeacherinfoEntityList.stream().collect(Collectors.groupingBy(e -> e.getEduDepartmentName()));
        List<EChartsDTO> chartsDTOList = new ArrayList<>();
        departmentMap.keySet().forEach(e -> {
            List<TEduTeacherinfoEntity> teacherinfoEntityList = departmentMap.get(e);
            if (CollectionUtils.isNotEmpty(teacherinfoEntityList)) {
                List<String> realNameList = teacherinfoEntityList.stream().map(j -> j.getRealName()).collect(Collectors.toList());
                EChartsDTO eChartsDTO = new EChartsDTO();
                eChartsDTO.setValue(teacherinfoEntityList.size());
                eChartsDTO.setName(e);
                eChartsDTO.setDataList(realNameList);
                chartsDTOList.add(eChartsDTO);
            }
        });
        return chartsDTOList;
    }

    @Override
    public void deleteTeacherInfoBySysUserId(Long[] userIds) {
        log.info("根据userId删除教师信息");
        QueryWrapper<TEduTeacherinfoEntity> wrapper = new QueryWrapper<TEduTeacherinfoEntity>();
        wrapper.in("sys_user_id", userIds);
        this.baseMapper.delete(wrapper);
    }


}
